package com.zxmc.server.service.impl;

import com.zxmc.aop.BloomFilterAdd;
import com.zxmc.aop.BloomFilterCheck;
import com.zxmc.common.context.BaseContext;
import com.zxmc.pojo.dto.NovelQueryDto;
import com.zxmc.pojo.entity.Chapter;
import com.zxmc.pojo.entity.Novel;
import com.zxmc.pojo.entity.ReadingHistory;
import com.zxmc.pojo.vo.ChapterDirectoryVO;
import com.zxmc.server.mapper.ChapterMapper;
import com.zxmc.server.service.ChapterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ChapterServiceImpl implements ChapterService {
    @Autowired
    private NovelServiceImpl novelService;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private ReadingHistoryServiceImpl readingHistoryService;

    @Override
    public void insert(Chapter chapter) {

        chapterMapper.insert(chapter);
    }

    @BloomFilterCheck
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Chapter list(Integer novelId, Integer chapterId) {
        //查询小说
        Novel novel = novelService.list(NovelQueryDto.builder().id(novelId).build()).get(0);
        Chapter chapter = chapterMapper.list(novelId, chapterId);
        //修改小说，更新人气
        Random random = new Random();
        // 生成一个 5 到 14 之间的随机整数
        int randomInt = random.nextInt(10)+5;
        novel.setWatchNum(novel.getWatchNum() + randomInt);
        novelService.update(novel);
        //增加浏览历史
        String key = "novelHistory:"+BaseContext.getCurrentId()+":"+novelId;
        ReadingHistory readingHistory = new ReadingHistory();
        readingHistory.setNovelId(novelId);
        readingHistory.setLastRead(chapterId);
        readingHistory.setUserId(BaseContext.getCurrentId());
        readingHistory.setLastReadTitle(chapter.getTitle());
        readingHistory.setLastReadTime(LocalDateTime.now());

        readingHistoryService.insert(key,readingHistory);
        //判断是否存在
        return chapter;

    }

    @Override
    public List<ChapterDirectoryVO> directory(Integer novelId) {
        //查询小说
        Novel novel = novelService.list(NovelQueryDto.builder().id(novelId).build()).get(0);
        Random random = new Random();
        // 生成一个 50 到 149 之间的随机整数
        int randomInt = random.nextInt(100)+50;
        //修改热度
        novel.setHot(novel.getHot() + randomInt);
        return chapterMapper.directory(novelId);
    }

    @Override
    public Integer getTotal(Integer novelId) {
        return chapterMapper.getTotal(novelId);
    }

    @Override
    public Chapter listOnly(Integer novelId, Integer chapterId) {
        return chapterMapper.list(novelId, chapterId);
    }

    @Override
    public List<Integer> listAllIds() {
        return chapterMapper.listAllIds();
    }
}
